package cn.com.linht.jsontosql.entity;


import cn.com.linht.jsontosql.util.Assert;
import org.apache.commons.collections4.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 查询条件
 *
 * @author linht
 */
public class QueryCriteriaVO {
    private String name;
    private String type;
    private Object value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "QueryCriteriaVO{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", value=" + value +
                '}';
    }

    public static Map<String, Object> listToMap(List<QueryCriteriaVO> queryCriteriaVOS) {
        if (CollectionUtils.isEmpty(queryCriteriaVOS)) {
            return new HashMap<>(1);
        }
        Map<String, Object> collect = queryCriteriaVOS.stream().collect(Collectors.toMap(QueryCriteriaVO::getName, QueryCriteriaVO::getValue));
        Assert.isTrue(collect.size() == queryCriteriaVOS.size(), "过滤条件有重复的name" + queryCriteriaVOS.toString());
        return collect;

    }
}
